<?php

namespace App\Controller\Api\Trade;

use App\Base\BaseController4Restful;
use App\Entity\Trade\ProductTrade;
use App\Repository\Trade\ProductTradeRepository;
use App\Repository\Trade\UserTradeRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ProductTradeApiController extends BaseController4Restful
{
    /**
     * @Route("/api/productTrades", name="apiGetProductTrades",methods={"GET","POST"})
     */
    public function apiGetProductTrades(ProductTradeRepository $repository,Request $request)
    {
        switch ($request->getMethod()) {
            case "POST":
                $data = $this->getRequestData();
                /**@var $ProductTrade ProductTrade*/
                $ProductTrade = $this->createEntity($data,ProductTrade::class);
                $this->myEm()->persist($ProductTrade);$this->myEm()->flush();
                return $this->json_out(1,"创建成功", $ProductTrade);
            default:
                $data       = $this->getRequestData();
                $per_page   = $data["limit"]??10;
                $page       = $data["page"]??1;
                $where      = $data['_where']??[];
                $search     = $data['_search']??[];
                $order      = $data["_order"]??["sort"=>"ASC"];
                $lang = $this->getLang();
                //列表筛选
                $qb = $repository->createQueryBuilder("A");
                $qb->where("A.lang = ".$lang->getId());
                createWhere($qb,$where);
                createSearch($qb,$search,[],"or");
                createOrderBy($qb,$order);
                $qb->setFirstResult($page*$per_page - $per_page)->setMaxResults($per_page);
                $list = $qb->getQuery()->execute();
                $list= $this->normalize($list);
                return $this->json_out(1,"获取成功", $list);
        }
    }

    /**
     * @Route("/api/productTrades/{id}", name="apiAddProductTrades",methods={"GET","PUT","PATCH","DELETE"})
     */
    public function apiUpdateProductTrades(ProductTrade $ProductTrade,Request $request,UserTradeRepository $userTradeRepository)
    {
        $data = $this->getRequestData();
        switch ($request->getMethod()) {
            case "DELETE":
                $this->myEm()->remove($ProductTrade);$this->myEm()->flush();
                return $this->json_out(1,"已移除");
            case "PATCH":
            case "PUT":
                /**@var $ProductTrade ProductTrade*/
                $ProductTrade = $this->updateEntity($data,ProductTrade::class,$ProductTrade);
                $this->myEm()->persist($ProductTrade);$this->myEm()->flush();
                break;
            case "GET":
                $user = $this->needToBeUser();
                //计算用户的最终折扣

                $tUser = $userTradeRepository->findOneBy(array("user"=>$user));
                $cut = $tUser->getSaleRate()?:100;

                //檢查优先级更高的分类折扣
                if ($ProductTrade->getCat()->getCut()) {
                    $cut = $ProductTrade->getCat()->getCut();
                }

                //商品本身优先级最高
                if ($ProductTrade->getUseCut()) {
                    $cut = $ProductTrade->getCut();
                }

                $ProductTrade = $this->toArray($ProductTrade);
                $arr = [];
                foreach ($ProductTrade["price"] as $value) {
                    $arr[] = round($value * $cut / 100 , 2);
                }
                $ProductTrade['salePrice'] = $arr;

//                dump($ProductTrade);
            default:
        }
        return $this->json_out(1,"获取成功", $ProductTrade);
    }
}
